//
//  NSObject+SYAdd.m
//  SYSDK
//
//  Created by 李国卿 on 2020/4/14.
//  Copyright © 2020 synones. All rights reserved.
//

#import "NSObject+SYAdd.h"
#import "SYMarcos.h"

@implementation NSObject (SYAdd)
- (UIViewController *)getCurrentVCFromWindow{
    UIViewController *result = nil;
    UIWindow *window = kAppWindow;
    if (window.windowLevel != UIWindowLevelNormal){
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows){
           if (tmpWin.windowLevel == UIWindowLevelNormal){
               window = tmpWin;
               break;
           }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]){
        result = nextResponder;
    }else{
       result = window.rootViewController;
    }
    return result;
}

- (UIViewController *)getCurrentVC{
    UIViewController  *superVC = [self getCurrentVCFromWindow];
    if ([superVC isKindOfClass:[UITabBarController class]]) {
       UIViewController  *tabSelectVC = ((UITabBarController*)superVC).selectedViewController;
       if ([tabSelectVC isKindOfClass:[UINavigationController class]]) {
           tabSelectVC = ((UINavigationController*)tabSelectVC).viewControllers.lastObject;
           return tabSelectVC;
       }
       return tabSelectVC;
    }else
       if ([superVC isKindOfClass:[UINavigationController class]]) {
           return ((UINavigationController*)superVC).viewControllers.lastObject;
       }
    return superVC;
}

@end
